package com.example.takeaway.util;

import cn.hutool.extra.mail.Mail;
import cn.hutool.extra.mail.MailAccount;
import com.sun.mail.util.MailSSLSocketFactory;
import lombok.SneakyThrows;

import java.security.GeneralSecurityException;

/**
 * @author 邢晨旭
 * {@code @date} 2023/10/12
 */
public class MailSender {

    private static final MailAccount MAIL_ACCOUNT;

    static {
        MAIL_ACCOUNT = new MailAccount();
        MAIL_ACCOUNT.setHost("smtp.qq.com");
        MAIL_ACCOUNT.setPort(465);
        MAIL_ACCOUNT.setFrom("xingchenxuup@qq.com");
        MAIL_ACCOUNT.setUser("xingchenxuup@qq.com");
        MAIL_ACCOUNT.setPass("euietvoxtiwwbgig");
        MAIL_ACCOUNT.setAuth(true);
        MAIL_ACCOUNT.setSslEnable(true);
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        }
        sf.setTrustAllHosts(true);
        MAIL_ACCOUNT.setCustomProperty("mail.smtp.ssl.socketFactory", sf);
    }

    @SneakyThrows
    public static void sendCode(String to, String code) {
        Mail.create(MAIL_ACCOUNT)
                .setTos(to)
                .setTitle("邮箱验证")
                .setContent(String.format("您的验证码是：<h3>%s</h3>", code))
                .setHtml(true)
                .send();
    }


    public static void sendTxt(String to, String subject, String content) {
        Mail.create(MAIL_ACCOUNT)
                .setTos(to)
                .setTitle(subject)
                .setContent(content)
                .setHtml(false)
                .send();
    }

    public static void sendHtml(String to, String subject, String content) {
        Mail.create(MAIL_ACCOUNT)
                .setTos(to)
                .setTitle(subject)
                .setContent(content)
                .setHtml(true)
                .send();
    }

}
